﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TcpTestServer.DataLayer;

namespace TcpTestServer
{
    class Program
    {
     
        static void Main(string[] args)
        {
           
            
          XmlProtocol Component1 = new XmlProtocol(222,"component1");
          int message_counter = 0;
             Component1.ConnectionEvent += new ConnectionDel(xml_ConnectionEvent); ;
           Component1.MessageSent += new MessageCallbackDel(xml_MessageSent);
           Component1.ResponseReceived += new MessageCallbackDel(xml_ResponseReceived);
            
           while (true)
           {
               
               System.Threading.Thread.Sleep(1200);
               if (Component1.Connected && Component1.IsReadyToSendMessage)
               {
                   
                   List<Argument> output_arg_lst = new List<Argument>();
                   List<Argument> reply_arg_lst = new List<Argument>();

                   output_arg_lst.Add(new Argument("first", "1245", DataTypeEnum.integer));
                   output_arg_lst.Add(new Argument("second", "aaaBBB", DataTypeEnum.text));
                   output_arg_lst.Add(new Argument("third", "3.21", DataTypeEnum.floating_point));

                   reply_arg_lst.Add(new Argument("first", DataTypeEnum.integer));
                   reply_arg_lst.Add(new Argument("second", DataTypeEnum.text));

                   Message m = new Message("get_gripping_point", output_arg_lst, reply_arg_lst, message_counter++,1000); 
                   Component1.MessageBuffer = m;
               }

           }
        }

        static void xml_ResponseReceived(object sender, MessageStatusArgs args)
        {
            ResponseMessageStatusArgs resp = (ResponseMessageStatusArgs)args;
            Console.WriteLine("Response received to message " + args.MessageNr.ToString() + " : " + resp.Status.ToString()  + " " + resp.Response);
       
        }
        static void xml_MessageSent(object sender, MessageStatusArgs args)
        {
            if (args.Status == MessageStatusEnum.successful)
                Console.WriteLine("Message sent " + (args.MessageNr).ToString());
            else
                Console.WriteLine("Message sending error");
        }
        static void xml_ConnectionEvent(object sender, ConnectionEventArgs args)
        {
            if (args.Status == ConnectionStatusEnum.connected)
            {
                Console.WriteLine("Component connected : " + args.ComponentName + " Componet's address : " + args.Address);
            }
            else if (args.Status == ConnectionStatusEnum.server_set_up)
            {
                Console.WriteLine("Server set up for component : " + args.ComponentName);
            }
            else if (args.Status == ConnectionStatusEnum.connection_error)
            {
                Console.WriteLine("Connection error : " + args.ErrorMessage);
            }
        }


      
    }
}
